package de.velvettear.bruteforce.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.border.EmptyBorder;

import de.velvettear.bruteforce.kernel.Database;
import de.velvettear.bruteforce.util.DirectoryHelper;
import de.velvettear.bruteforce.util.Log;

public class Splash extends JFrame {
	
	private JPanel contentPane;
	private JProgressBar progressbar;
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Splash frame = new Splash();
					frame.setVisible(true);
				} catch (Exception e) {
					Log.error(e);
				}
			}
		}); 
	}

	public Splash() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 500, 400);
		contentPane = new JPanel();
		contentPane.setBackground(Color.BLACK);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		setUndecorated(true);
		
		progressbar = new JProgressBar();
		progressbar.setForeground(Color.RED);
		progressbar.setMaximum(100);
		contentPane.add(progressbar, BorderLayout.SOUTH);
		
		pack();
		
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation(dim.width / 2 - this.getSize().width / 2, dim.height / 2 - this.getSize().height / 2);
		
		init();
	}
	
	private void init() {
		Thread loadThread = new Thread(new Runnable() {			
			@Override
			public void run() {
				try {
					Thread.sleep(250);
					progressbar.setValue(10);
					Thread.sleep(500);
					DirectoryHelper.createDirectories();
					progressbar.setValue(25);
					Thread.sleep(750);
					Database.loadDriver();
					progressbar.setValue(50);
					Thread.sleep(350);
					Menu menu = new Menu();
					progressbar.setValue(85);
					Thread.sleep(250);
					progressbar.setValue(100);
					Thread.sleep(150);
										
					menu.setVisible(true);
					Splash.this.setVisible(false);		
				} catch (Exception e) {
					Log.error(e);
				}						
			}
		});
		loadThread.start();
	}
}
